#include "artefact.h"

namespace IAlib
{
    Artefact::Artefact(const QString id)
    {
        m_id = id;
        m_owner_id = "";
        m_type = ArtefactType_OTHER;
        m_class = "default";
        m_state = ArtefactState_UNKNOWN;
        m_exclusive = true;
        m_max_slot = 1;
    }

    Artefact::Artefact(const Artefact &artefact)
    {
        m_id = artefact.m_id;
        m_owner_id = artefact.m_owner_id;
        m_type = artefact.m_type;
        m_class = artefact.m_class;
        m_state = artefact.m_state;
        m_exclusive = artefact.m_exclusive;
        m_max_slot = artefact.m_max_slot;
        m_position = artefact.m_position;
    }
    void Artefact::setPosition(const float x, const float y, const float z, const QString &work_space_id)
    {
        m_position.m_x = x;
        m_position.m_y = y;
        m_position.m_z = z;
        m_position.setWorkSpaceId(work_space_id);
    }

    QDomElement Artefact::toXML(QDomDocument &doc)
    {
        QDomElement ele = doc.createElement("artefact");
        ele.setAttribute("artefactID", m_id);
        ele.setAttribute("ownerID", m_owner_id);
        ele.setAttribute("artefactType", m_type);
        ele.setAttribute("artefactClass", m_class);
        ele.setAttribute("artefactExclusive", m_exclusive);
        ele.setAttribute("artefactMaxSlot", m_max_slot);
        ele.appendChild(m_position.toXML(doc));
        return ele;
    }
    void Artefact::fromXML(QDomElement ele)
    {
        setID(ele.attribute("artefactID",""));
        setOwnerId(ele.attribute("ownerID",""));
        setArtefactType((ArtefactType)ele.attribute("artefactType","").toInt());
        setClass(ele.attribute("artefactClass",""));
        QString exclusive = ele.attribute("artefactExclusive","");
        if (exclusive == "1")
        {
            setExclusive(true);
        }
        else
        {
            setExclusive(false);
        }
        setMaxSlot(ele.attribute("artefactMaxSlot","").toInt());
        QDomNode n = ele.firstChild();
        m_position.fromXML(n.toElement());
    }
}
